package cn.zhangxin.pethome.system.verifycode.service.impl;

import cn.zhangxin.pethome.constants.BaseConstants;
import cn.zhangxin.pethome.system.user.domain.User;
import cn.zhangxin.pethome.system.user.mapper.UserMapper;
import cn.zhangxin.pethome.system.user.service.IUserService;
import cn.zhangxin.pethome.system.verifycode.domain.LogOnMessage;
import cn.zhangxin.pethome.system.verifycode.service.IVerifyCodeService;
import cn.zhangxin.pethome.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisTemplate redisTemplate;//注入redis
    @Autowired
    private UserMapper userMapper;
    @Value("sendMsg_Uid")
    private String Uid;
    @Value("sendMsg_Key")
    private String Key;

    @Override
    public String getImgCode(String imageCodeKey) {

        try {
            String code = StrUtils.getRandomString(6);//随机获得6位数的数字
            redisTemplate.opsForValue().set(imageCodeKey,code,5, TimeUnit.MINUTES);//将key和验证码存入redis中
            //String o = (String)redisTemplate.opsForValue().get(imageCodeKey);
            //System.out.println(o);
            String getImgCode = VerifyCodeUtils.VerifyCode(110, 40, code);//生成验证码图片base64
            return getImgCode;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("m生成验证码失败");
        }
        return "验证码生成失败，请联系管理员！！！";
    }

    //发送短信
    @Override
    public void sendMessage(LogOnMessage logOnMessage) {
        //校验手机号是否为空
        if ("".equals(logOnMessage.getPhone().trim())){
            throw new BusinessException("手机号不能为空");
        }
        //校验手机号验证是否合法
        if(!MobileUtil.checkPhone(logOnMessage.getPhone())){
            throw new BusinessException("请输入正确的手机号");
        }
        //校验该账号是否已经被注册
        User phone1 = userMapper.findPhone(logOnMessage.getPhone());
        if (phone1!=null){
            throw new BusinessException("该账号已经注册请登录");
        }
        //校验验证码是否为空
        if(logOnMessage.getImageCode()==null){
            throw new BusinessException("图片验证码为空");
        }
        //校验验证码是否过期
        String RedisImageCode= (String)redisTemplate.opsForValue().get(logOnMessage.getImageCodeKey());
        if (RedisImageCode==null){
            throw new BusinessException("图片验证码已过期");
        }
        //校验验证码是否合法
        if(!RedisImageCode.equals(logOnMessage.getImageCode())){
            throw new BusinessException("图片验证码错误");
        }
        sendMessage1(logOnMessage,BaseConstants.Redis.REGISTER,BaseConstants.Redis.REGISTERTime);
    }
    //绑定发短信
    @Override
    public void sendSmsCodeBind(LogOnMessage logOnMessage) {
        //校验手机号是否为空
        if ("".equals(logOnMessage.getPhone().trim())){
            throw new BusinessException("手机号不能为空");
        }
        //校验手机号验证是否合法
        if(!MobileUtil.checkPhone(logOnMessage.getPhone())){
            throw new BusinessException("请输入正确的手机号");
        }
        sendMessage1(logOnMessage,BaseConstants.Redis.BIND,BaseConstants.Redis.REGISTERTime);
    }

    /* 发短信方法 */
    @Override
    public void sendMessage1(LogOnMessage logOnMessage,String key ,String keyTime) {
        String phone = logOnMessage.getPhone();
        String vCode = (String)redisTemplate.opsForValue().get(phone + key);
        if (vCode==null){
            //生成一个6位数验证码
            String randomString = StrUtils.getRandomString(6);
            //将短信验证码存入redis
            redisTemplate.opsForValue().set(phone+key,randomString,3,TimeUnit.MINUTES);
            //String o = (String) redisTemplate.opsForValue().get(phone + key);
            //System.out.println("短信验证码" +o);
            //将发送短信验证码的时间戳存入redis
            redisTemplate.opsForValue().set(phone+keyTime,System.currentTimeMillis(),3,TimeUnit.MINUTES);
            System.out.println(randomString);
            //发送短信
            //SendMsgUtils.getSentMsg(Uid,Key,logOnMessage.getPhone(),randomString);
        }else {
            //当redis中有验证码
            Long  vCodeTime= (Long)redisTemplate.opsForValue().get(phone + keyTime);
            if( System.currentTimeMillis()-vCodeTime>60000){
                System.out.println(vCode);
                //发送短信
                //SendMsgUtils.getSentMsg(Uid,Key,logOnMessage.getPhone(),vCode);
            }else {//恶意请求
                throw new BusinessException("恶意攻击，牢底坐穿");
            }
        }
    }
}
